#define _CRT_SECURE_NO_WARNINGS 1
#include "Sort.h"
#include <string.h>
void Swap(int* p, int* q)
{
	int tmp = *p;
	*p = *q;
	*q = tmp;
}

void print(int* arr, int n)
{
	for (int i = 0;i < n;i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}

void BubbleSort(int* arr, int n)
{
	for (int i = 0;i < n-1;i++)
	{
		int flag = 1;
		for (int j = 0;j < n - 1 - i;j++)
		{
			if (arr[j] > arr[j + 1])
			{
				Swap(&arr[j], &arr[j + 1]);
			}
			flag = 0;
		}
		if (flag == 1)
		{
			break;
		}
	}
}

void SelectSort(int* arr, int n)
{
	int begin = 0, end = n - 1;
	while (begin < end)
	{
		int maxi = begin;
		int mini = begin;
		for (int i = begin + 1;i <= end;i++)
		{
			if (arr[i] > arr[maxi])
			{
				maxi = i;
			}
			if (arr[i] < arr[mini])
			{
				mini = i;
			}
		}
		Swap(&arr[begin], &arr[mini]);
		if (begin == maxi)
		{
			maxi = mini;
		}
		Swap(&arr[end], &arr[maxi]);
		begin++;
		end--;
	}
}

void InsertSort(int* arr, int n)
{
	// 最后一个插入的是n - 1，所以i最多到n - 2
	for (int i = 0;i < n - 1;i++)
	{
		int end = i;
		int tmp = arr[end + 1];
		//[0,end]有序 end + 1 插入
		while (end >=0)
		{
			if (tmp < arr[end])
			{
				arr[end + 1] = arr[end];
				end--;
			}
			else
			{
				break;
			}
		}
		arr[end + 1] = tmp;
	}
}

static void AdjustDown(int* arr, int n, int parents)
{
	int child = parents * 2 + 1;
	while (child < n)
	{
		if ((child + 1 < n) && (arr[child + 1] > arr[child]))
		{
			child++;
		}
		if (arr[child] > arr[parents])
		{
			Swap(&arr[child], &arr[parents]);
		}
		parents = child;
		child = parents * 2 + 1;
	}
}

void HeapSort(int* arr, int n)
{
	//先建堆
	for (int i = (n - 1 - 1) / 2;i >= 0;i--)
	{
		AdjustDown(arr, n, i);
	}
	//借助堆删除的思想
	int end = n - 1;
	while (end > 0)
	{
		Swap(&arr[0], &arr[end]);
		AdjustDown(arr, end, 0);
		end--;
	}
}

//希儿排序的核心思想是1.预排序 2.插入排序
void ShellSort1(int* arr, int n)
{
	int gap = n;
	while (gap > 1)	// gap此时
	{
		gap = gap / 3 + 1;
		for (int i = 0;i < n - gap;i++)
		{
			int end = i;
			int tmp = arr[end + gap];
			while (end >= 0)
			{
				if (tmp < arr[end])
				{
					arr[end + gap] = arr[end];
					end -= gap;
				}
				else
				{
					break;
				}
			}
			arr[end + gap] = tmp;
		}
	}
}

void ShellSort2(int* arr, int n)
{
	int gap = n;
	while (gap > 1)
	{
		gap = gap / 3 + 1;
		for (int j = 0;j < gap; j++)
		{
			for (int i = j;i < n - gap;i += gap)
			{
				int end = i;
				int tmp = arr[end + gap];
				while (end >= 0)
				{
					if (tmp < arr[end])
					{
						arr[end + gap] = arr[end];
						end -= gap;
					}
					else
					{
						break;
					}
				}
				arr[end + gap] = tmp;
			}
		}
	}
}

// 三数取中
static int GetMiddle(int* arr, int left, int right)
{
	int middle = left + (right - left) / 2;
	if (arr[left] < arr[middle])
	{
		if (arr[middle] < arr[right])
		{
			return middle;
		}
		else if (arr[left] < arr[right])
		{
			return right;
		}
		else
		{
			return left;
		}
	}
	else //逻辑是left 比 middle大
	{
		if (arr[middle] > arr[right])
		{
			return middle;
		}
		else if (arr[left] > arr[right])
		{
			return right;
		}
		else
		{
			return left;
		}
	}
}

// 快排的核心思想是：分治
// 需要思考两个问题 1.为什么右边先走 2.为什么相遇点总是小于keyi
// 需要进行两次优化 1.三数取中 2.小区间优化
void QuickSort1(int* arr, int left, int right)
{
	if (left >= right)
	{
		return;
	}
	if (right - left + 1 < 10)
	{
		InsertSort(arr + left, right - left + 1);	//注意这里插入排序的起始地址
		return;
	}
	int mid = GetMiddle(arr, left, right);
	Swap(&arr[mid], &arr[left]);
	int keyi = left;
	int begin = left, end = right;
	while (begin < end)
	{
		// 右边先走
		while ((begin < end) && (arr[end] >= arr[keyi]))
		{
			end--;
		}
		while ((begin < end) && (arr[begin] <= arr[keyi]))
		{
			begin++;
		}
		Swap(&arr[begin], &arr[end]);
	}
	Swap(&arr[keyi], &arr[begin]);
	keyi = begin;
	// [left, keyi - 1] keyi [keyi + 1, right]
	QuickSort1(arr, left, keyi - 1);
	QuickSort1(arr, keyi + 1, right);
}

void QuickSort2(int* arr, int left, int right)
{
	if (left >= right)
	{
		return;
	}
	if (right - left + 1 < 10)
	{
		InsertSort(arr + left, right - left + 1);
		return;
	}
	int mid = GetMiddle(arr, left, right);
	Swap(&arr[mid], &arr[left]);
	int keyi = left;
	int key = arr[left];
	int begin = left, end = right;
	while (begin < end)
	{
		while ((begin < end) && (arr[end] >= key))
		{
			end--;
		}
		arr[keyi] = arr[end];
		keyi = end;
		while ((begin < end) && (arr[begin] <= key))
		{
			begin++;
		}
		arr[keyi] = arr[begin];
		keyi = begin;
	}
	arr[keyi] = key;
	QuickSort2(arr, left, keyi - 1);
	QuickSort2(arr, keyi + 1, right);
}

void QuickSort3(int* arr, int left, int right)
{
	if (left >= right)
	{
		return;
	}
	if (right - left + 1 < 10)
	{
		InsertSort(arr + left, right - left + 1);
		return;
	}
	int mid = GetMiddle(arr, left, right);
	int keyi = left;
	Swap(&arr[mid], &arr[left]);
	int prev = left, cur = left + 1;
	while (cur <= right)
	{
		if ((arr[cur] < arr[keyi]) && (++prev != cur))
		{
			Swap(&arr[prev], &arr[cur]);
		}
		cur++;
	}
	Swap(&arr[keyi], &arr[prev]);
	keyi = prev;
	// [left, keyi - 1] keyi [keyi + 1, right] 
	QuickSort3(arr, left, keyi - 1);
	QuickSort3(arr, keyi + 1, right);
}

void QuickSort4(int* arr, int left, int right)
{
	if (left >= right)
	{
		return;
	}
	if (right - left + 1 < 10)
	{
		InsertSort(arr + left, right - left + 1);
		return;
	}
	int begin = left, end = right;
	int mid = GetMiddle(arr, left, right);
	int key = arr[left];
	Swap(&arr[mid], &arr[left]);
	int cur = left + 1;
	while (cur <= right)
	{
		if (arr[cur] < key)
		{
			Swap(&arr[cur], &arr[left]);
			left++;
			cur++;
		}
		else if (arr[cur] > key)
		{
			Swap(&arr[cur], &arr[right]);
			right--;
		}
		else
		{
			cur++;
		}
	}
	// [begin, left - 1] keyi [right + 1, end] 
	QuickSort4(arr, begin, left - 1);
	QuickSort4(arr, right + 1, end);
}

void QuickSortR1(int* arr, int left, int right)
{
	ST s;
	STInit(&s);
	// 注意是先入右，再入左。出的时候就是先左后右了。
	STPush(&s, right);
	STPush(&s, left);
	while (!STEmpty(&s))
	{
		int begin = STTop(&s);
		STPop(&s);
		int end = STTop(&s);
		STPop(&s);

		// 以下是双指针的过程
		int mid = GetMiddle(arr, begin, end);
		int keyi = begin;
		Swap(&arr[mid], &arr[begin]);
		int prev = begin, cur = begin + 1;
		while (cur <= end)
		{
			if ((arr[cur] < arr[keyi]) && (++prev != cur))
			{
				Swap(&arr[prev], &arr[cur]);
			}
			cur++;
		}
		Swap(&arr[keyi], &arr[prev]);
		keyi = prev;

		// [begin, keyi - 1] keyi [keyi + 1, end]
		if (keyi + 1 < end)
		{
			STPush(&s, end);
			STPush(&s, keyi + 1);
		}
		if (begin < keyi - 1)
		{
			STPush(&s, keyi - 1);
			STPush(&s, begin);
		}
	}
	STDestroy(&s);
}

void QuicKSortR2(int* arr, int left, int right)
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, left);
	QueuePush(&q, right);
	while (!QueueEmpty(&q))
	{
		int begin = QueueFront(&q);
		QueuePop(&q);
		int end = QueueFront(&q);
		QueuePop(&q);

		// 以下是双指针的过程
		int mid = GetMiddle(arr, begin, end);
		int keyi = begin;
		Swap(&arr[begin], &arr[mid]);
		int prev = begin, cur = begin + 1;
		while (cur <= end)
		{
			if ((arr[cur] < arr[keyi]) && (++prev != cur))
			{
				Swap(&arr[prev], &arr[cur]);
			}
			cur++;
		}
		Swap(&arr[keyi], &arr[prev]);
		keyi = prev;

		// [begin, keyi - 1] keyi [keyi + 1, end]
		if (begin < keyi - 1)
		{
			QueuePush(&q, begin);
			QueuePush(&q, keyi - 1);
		}
		if (keyi + 1 < end)
		{
			QueuePush(&q, keyi + 1);
			QueuePush(&q, end);
		}
	}
	QueueDestroy(&q);
}

// 归并排序的核心是：后序
// 只要左子树有序，右子树有序，那就可以归并。
void _MergeSort(int* arr, int* tmp, int left, int right)
{
	if (left >= right)
	{
		return;
	}
	int mid = left + (right - left) / 2;
	// [left, mid] [mid + 1, right]
	_MergeSort(arr, tmp, left, mid);
	_MergeSort(arr, tmp, mid + 1, right);

	// 归并
	int begin1 = left, end1 = mid;
	int begin2 = mid + 1, end2 = right;
	int i = begin1;
	while ((begin1 <= end1) && (begin2 <= end2))
	{
		if (arr[begin1] <= arr[begin2])	// 注意这里是<=是确保其稳定性的
		{
			tmp[i++] = arr[begin1++];
		}
		else
		{
			tmp[i++] = arr[begin2++];
		}
	}
	while (begin1 <= end1)
	{
		tmp[i++] = arr[begin1++];
	}
	while (begin2 <= end2)
	{
		tmp[i++] = arr[begin2++];

	}
	memcpy(arr + left, tmp + left, (right - left + 1) * sizeof(int));	// 注意这里是左闭右闭，右区间 - 左区间需要再+1
}

void MergeSort(int* arr, int n)
{
	int* tmp = (int*)malloc(n * sizeof(int));
	if (tmp == NULL)
	{
		perror("malloc fail!\n");
		exit(0);
	}
	_MergeSort(arr, tmp, 0, n - 1);
	free(tmp);
	tmp = NULL;
}

// gap是一组的数据个数，每次排两组。
void MergeSortR(int* arr, int n)
{
	int* tmp = (int*)malloc(n * sizeof(int));
	if (tmp == NULL)
	{
		perror("malloc fail!\n");
		exit(0);
	}
	int gap = 1;
	while (gap < n)
	{
		for (int i = 0;i < n;i += 2 * gap)
		{
			int begin1 = i, end1 = i + gap - 1;
			int begin2 = i + gap, end2 = i + 2 * gap - 1;
			int j = begin1;
			// 出现需要纠正的就两种情况
			// 要么begin2越界，那第二组就不需要和第一组进行归并了，因为第一组本身就是有序的
			// 要么end2越界，这时候第二组没越界的元素需要和第一组进行归并，那就矫正end2 = n - 1
			// 为什么能确定end2就是n-1呢？显然，这种越界情况只会出现在最后一组
			if (begin2 >= n)
			{
				break;
			}
			if (end2 >= n)
			{
				end2 = n - 1;
			}
			while ((begin1 <= end1) && (begin2 <= end2))
			{
				if (arr[begin1] <= arr[begin2])
				{
					tmp[j++] = arr[begin1++];
				}
				else
				{
					tmp[j++] = arr[begin2++];
				}
			}
			while (begin1 <= end1)
			{
				tmp[j++] = arr[begin1++];
			}
			while (begin2 <= end2)
			{
				tmp[j++] = arr[begin2++];
			}
			memcpy(arr + i, tmp + i, (end2 - i + 1) * sizeof(int));	// 注意这里，begin1是不断变化的，不能用end2-beign1，而是要用i
		}
		gap *= 2;
	}
	free(tmp);
	tmp = NULL;
}